package cn.edu.nju.software.android.model;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.maxwin.view.IListviewCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import cn.edu.nju.software.android.activity.MainActivity;
import cn.edu.nju.software.android.util.DateHelper;

import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.CommentsAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.api.WeiboAPI.AUTHOR_FILTER;
import com.weibo.sdk.android.net.RequestListener;


public class Comment implements Serializable {
	public static CommentsAPI api = new CommentsAPI(MainActivity.accessToken);
	private static final long serialVersionUID = 8263605729742220214L;
	private Date createdAt;
	private long id;
	private String text;
	private User user;
	private Status status;
	private Comment reply_comment;

	public Comment(){

	}

	public Comment(String text){
		this.text = text;
	}

	public Comment(JSONObject json){
		try {
			id = json.getLong("id");
			text = json.getString("text");
			createdAt = DateHelper.parseDate(json.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
			if(!json.isNull("user")){
				user = new User(json.getJSONObject("user"));
			}
			if(!json.isNull("status")){
				status = new Status(json.getJSONObject("status"));
			}
			if(!json.isNull("reply_comment")){
				setReply_comment(new Comment(json.getJSONObject("reply_comment")));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	public static List<Comment> constructComments(String res) {
		ArrayList<Comment> comments = new ArrayList<Comment>();
		JSONArray json;
		try {
			JSONObject object = new JSONObject(res);
			json = object.getJSONArray("comments");
			for (int i = 0; i < json.length(); i++) {
				Comment c = new Comment(json.getJSONObject(i));
				comments.add(c);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return comments;
	}


	public Date getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public Comment getReply_comment() {
		return reply_comment;
	}
	public void setReply_comment(Comment reply_comment) {
		this.reply_comment = reply_comment;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public static void downloadMoreComments(long statusID, int pageNum, final List<Comment> comments, final Handler handler) {
		api.show(statusID, 0, 0, 20, pageNum, AUTHOR_FILTER.ALL, new RequestListener() {

			@Override
			public void onIOException(IOException e) {

			}

			@Override
			public void onError(WeiboException e) {
				System.out.println(e);
			}

			@Override
			public void onComplete(String response) {
				List<Comment> newComments = Comment.constructComments(response);
				for(int i = 0; i < newComments.size() ; i++){
					comments.add(newComments.get(i));
				}
				handler.sendEmptyMessage(newComments.size());
			}
		});
	}

	public void postNewComment(long id, final Handler handler){
		api.create(text, id, true, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response) {
				handler.sendEmptyMessage(0);
			}
		});
	}

	public void replyComment(long comment_id, long  status_id, final Handler handler){
		api.reply(comment_id, status_id, text, false, true, new RequestListener() {

			@Override
			public void onIOException(IOException e) {

			}

			@Override
			public void onError(WeiboException e) {

			}

			@Override
			public void onComplete(String response) {
				handler.sendEmptyMessage(0);
			}
		});
	}

	public static void downloadNewestToMeComments(final long since_id, final List<Comment> comments, final Handler handler){
		api.toME(since_id, 0, 50, 1, WeiboAPI.AUTHOR_FILTER.ALL, WeiboAPI.SRC_FILTER.ALL, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response) {
				int newNum = 0;
				List<Comment> newComments = Comment.constructComments(response);
				for(int i = newComments.size()-1; i >= 0 ; i--){
					Comment comment = newComments.get(i);
					if(comment.getId() != since_id){
						newNum++;
						comments.add(0, comment);
					}
				}
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("type", IListviewCallback.REFRESH);
				bundle.putInt("num", newNum);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}

	public static void downloadMoreToMeComments(final long max_id, final List<Comment> comments, final Handler handler){
		api.toME(0, max_id, 50, 1, WeiboAPI.AUTHOR_FILTER.ALL, WeiboAPI.SRC_FILTER.ALL, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response) {
				int newNum = 0;
				List<Comment> newComments = Comment.constructComments(response);
				for(int i = 0; i < newComments.size() ; i++){
					Comment comment = newComments.get(i);
					if(comment.getId() != max_id){
						newNum++;
						comments.add(comment);
					}
				}
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("type", IListviewCallback.LOAD_MORE);
				bundle.putInt("num", newNum);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}

	public static void downloadNewestMentionMeComments(final long since_id, final List<Comment> comments, final Handler handler){
		api.mentions(since_id, 0, 50, 1, WeiboAPI.AUTHOR_FILTER.ALL, WeiboAPI.SRC_FILTER.ALL, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response) {
				int newNum = 0;
				List<Comment> newComments = Comment.constructComments(response);
				for(int i = newComments.size()-1; i >= 0 ; i--){
					Comment comment = newComments.get(i);
					if(comment.getId() != since_id){
						newNum++;
						comments.add(0, comment);
					}
				}
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("type", IListviewCallback.REFRESH);
				bundle.putInt("num", newNum);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}

	public static void downloadMoreMentionsMeComments(final long max_id, final List<Comment> comments, final Handler handler){
		api.mentions(0, max_id, 50, 1, WeiboAPI.AUTHOR_FILTER.ALL, WeiboAPI.SRC_FILTER.ALL, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response) {
				int newNum = 0;
				List<Comment> newComments = Comment.constructComments(response);
				for(int i = 0; i < newComments.size() ; i++){
					Comment comment = newComments.get(i);
					if(comment.getId() != max_id){
						newNum++;
						comments.add(comment);
					}
				}
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("type", IListviewCallback.LOAD_MORE);
				bundle.putInt("num", newNum);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}


}